﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WLFramework.Data;
using WLFramework.Linq;
using System.Data.SqlClient;
using WLFramework.Data.Linq;
using System.Diagnostics;
using WLFramework.DataAccess;
using System.Collections.Generic;
using WLFramework.Mapping;

namespace UnitTest1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            string encrypt = WLFramework.Security.AES.AESEncrypt("Hello");
            string orgin = WLFramework.Security.AES.AESDecrypt(encrypt);

            Assert.AreEqual(orgin, "Hello");
        }

        public class sys_UserInfo 
        {
            [PrimaryKey, Identity]
            public int U_ID { get; set; }
            public string U_CName { get; set; }
        }

        [TestMethod]
        public void TestMethod2()
        {
            using (SqlConnection conn = new SqlConnection("Data Source=192.168.4.34;Initial Catalog=xkDB;User ID=sa;Password=123456"))
            using (DbManager manager = new DbManager(conn))
            {
                var table = manager.GetTable<sys_UserInfo>();
                
                var query = from p in table
                            where Sql.Like(p.U_CName, "_试")
                            select p;
                var list = query.ToList();

                if (list.Count > 0)
                {
 
                }
            }
        }

        public class test
        {
            [PrimaryKey, Identity]
            public int Id { get; set; }
            public string Name { get; set; }
            public string Remark { get; set; }

            [Association(ThisKey = "Id", OtherKey = "test_id")]
            public IEnumerable<test_child> children { get; set; }
        }

        public class test_child
        {
            [PrimaryKey, Identity]
            public int id { get; set; }
            
            public int test_id { get; set; }

            public string child_name { get; set; }

            [Association(ThisKey="test_id", OtherKey="Id")]
            public test test { get; set; }
        }

        [TestMethod]
        public void TestMethod3()
        {
            List<test> list = new List<test>();

            for (int i = 0; i < 10000; i++)
            {
                list.Add(new test() { Name = "test" + (i + 1).ToString() });
            }

            Stopwatch watch = new Stopwatch();
            watch.Start();

            using (SqlConnection conn = new SqlConnection("Data Source=192.168.4.34;Initial Catalog=mytest;User ID=sa;Password=123456"))
            using (DbManager manager = new DbManager(conn))
            {
                manager.BeginTransaction();
                //foreach (var i in list)
                //{
                //    manager.Insert(i);
                //}
                //manager.InsertBatch(list);

                var t = new test()
                {
                    Name = "test",
                    Remark = "haha",
                    children = new List<test_child>
                    {
                        new test_child(){ child_name="child01" },
                        new test_child(){ child_name="child02" },
                        new test_child(){ child_name="child03" },
                        new test_child(){ child_name="child04" },
                    }
                };

                t.Id = Convert.ToInt32(manager.InsertWithIdentity(t));
                foreach (var i in t.children)
                {
                    i.test_id = t.Id;
                }

                manager.InsertBatch(t.children);
                
                manager.CommitTransaction();
            }

            Assert.Inconclusive("takes " + watch.ElapsedMilliseconds + "ms");
        }
    }
}
